/*
 * PanelBusquedaSimple.java
 *
 * Created on 19 de diciembre de 2007, 19:58
 */
package com.codeko.apps.campanilla.ignotus;

import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.DefaultListModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author  campanilla
 */
public class PanelBusquedaSimple extends javax.swing.JPanel {

    DefaultListModel modelo = new DefaultListModel();

    /** Creates new form PanelBusquedaSimple */
    public PanelBusquedaSimple() {
        listenerResultados = new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent arg0) {
                listaSeleccionada();
            }
        };
        initComponents();
        buscador.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if ("nuevaBusqueda".equals(evt.getPropertyName())) {
                    modelo.clear();
                } else if ("addLibro".equals(evt.getPropertyName())) {
                    modelo.addElement(evt.getNewValue());
                }
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lBuscar = new javax.swing.JLabel();
        tfBuscar = new javax.swing.JTextField();
        scListaBuscar = new javax.swing.JScrollPane();
        listaBuscar = new javax.swing.JList();
        listaBuscar.setCellRenderer(new com.codeko.apps.campanilla.ignotus.RendererLibroBusquedaSimple());
        listaBuscar.getSelectionModel().addListSelectionListener(listenerResultados);

        setName("Form"); // NOI18N
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                formAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.codeko.apps.campanilla.ignotus.IgnotusApp.class).getContext().getResourceMap(PanelBusquedaSimple.class);
        lBuscar.setText(resourceMap.getString("lBuscar.text")); // NOI18N
        lBuscar.setToolTipText(resourceMap.getString("lBuscar.toolTipText")); // NOI18N
        lBuscar.setName("lBuscar"); // NOI18N

        tfBuscar.setText(resourceMap.getString("tfBuscar.text")); // NOI18N
        tfBuscar.setName("tfBuscar"); // NOI18N
        tfBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tfBuscarKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tfBuscarKeyTyped(evt);
            }
        });

        scListaBuscar.setName("scListaBuscar"); // NOI18N

        listaBuscar.setModel(modelo);
        listaBuscar.setName("listaBuscar"); // NOI18N
        listaBuscar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listaBuscarMouseClicked(evt);
            }
        });
        listaBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                listaBuscarKeyReleased(evt);
            }
        });
        scListaBuscar.setViewportView(listaBuscar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(scListaBuscar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 468, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lBuscar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lBuscar)
                    .addComponent(tfBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scListaBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tfBuscarKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfBuscarKeyTyped
        buscador.buscar(tfBuscar.getText() + evt.getKeyChar());
    }//GEN-LAST:event_tfBuscarKeyTyped
    private void cerrar() {
        SwingUtilities.getWindowAncestor(this).dispose();
    }
    private void listaBuscarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaBuscarMouseClicked
        if (evt.getClickCount() == 2) {
            cerrar();
        }
    }//GEN-LAST:event_listaBuscarMouseClicked
    private void listaSeleccionada() {
        Object val = listaBuscar.getSelectedValue();
        if (val != null && val instanceof Libro) {
            ((IgnotusView) IgnotusApp.getApplication().getMainView()).setLibro((Libro) val);
        }
    }
    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        tfBuscar.requestFocus();
    }//GEN-LAST:event_formComponentShown

    private void tfBuscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfBuscarKeyReleased
        int i = listaBuscar.getSelectedIndex();
        if (evt.getKeyCode() == KeyEvent.VK_UP && i > 0) {
            listaBuscar.setSelectedIndex(i - 1);
            listaBuscar.ensureIndexIsVisible(i - 1);
        } else if (evt.getKeyCode() == KeyEvent.VK_DOWN && i < listaBuscar.getModel().getSize()) {
            listaBuscar.setSelectedIndex(i + 1);
            listaBuscar.ensureIndexIsVisible(i + 1);
        } else if (evt.getKeyCode() == KeyEvent.VK_ENTER && i != -1) {
            cerrar();
        }
    }//GEN-LAST:event_tfBuscarKeyReleased

    private void listaBuscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_listaBuscarKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER && listaBuscar.getSelectedIndex() != -1) {
            cerrar();
        }
    }//GEN-LAST:event_listaBuscarKeyReleased

    private void formAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_formAncestorAdded
        buscador.buscar("_%");
    }//GEN-LAST:event_formAncestorAdded
    private BusquedaLibros buscador = new BusquedaLibros();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lBuscar;
    private javax.swing.JList listaBuscar;
    private javax.swing.JScrollPane scListaBuscar;
    private javax.swing.JTextField tfBuscar;
    // End of variables declaration//GEN-END:variables
    private ListSelectionListener listenerResultados;
}
